ROOT_DIR="/home/mediaresource/网页资源文件/02多媒体测试源(未整理)"
#ROOT_DIR="/home/mediaresource/网页资源文件/02多媒体测试源(未整理)/fate-suite.ffmpeg.org"
#ROOT_DIR="/home/mediaresource/网页资源文件/02多媒体测试源(未整理)/Eswin测试资源/new"
DST_DIR=`pwd`
ENABLE_NO_CODEC_FILE=0
SUB_PROCESS_NUM=100
echo "root dir: $ROOT_DIR"
if [ -n "$1" ];then
    file_list="$1"
else
    file_list="`find "$ROOT_DIR" -type f`"
fi

debug=0
if [[ "$2" = *debug ]] || [[ "$1" = *debug ]];then
    set -x
    debug=1
fi

tmp_fifofile=`mktemp`
rm -f $tmp_fifofile
tmp_fifofile="$tmp_fifofile.fifo"
mkfifo $tmp_fifofile
exec 6<>$tmp_fifofile
rm -f $tmp_fifofile

for ((i=0;i<$SUB_PROCESS_NUM;i++));do
echo
done >&6

while read f
do
read -u6
{
    ffmpeg_info="`ffprobe -i "$f" 2>&1`"
    container_info=`echo "$ffmpeg_info"|grep "Input #0"|awk '{print $3}'`
    if [ -n "$container_info" ];then
        container=`echo "$container_info"|awk '{print substr($0,0,length($0)-1)}'`
        streams_infos=`echo "$ffmpeg_info"|grep " Stream #0:[0-9]"`
        echo "$streams_infos"|while read stream_info
        do
	    if [ -z "$stream_info" ]; then
                echo "$f was no codec info, skip"
                continue
            fi
            split_type_chars=`echo "$stream_info"|grep -oP ': \w+:'`
            media_type=`echo "$split_type_chars"|awk -F":" '{print $2}'|awk '{print $1}'`
            codec=`echo "$stream_info"|awk -F"$split_type_chars" '{print $2}'|awk '{print $1}'`
            if [ -z "$codec" ]; then
                echo "$f no codec info, skip"
                continue
            fi
            #remove the last ","    
            if echo "$codec"|grep ",$" 1>/dev/null ; then
                codec=`echo "$codec"|awk '{print substr($0,0,length($0)-1)}'`
            fi
            #skip regular text or log files
            if [ "$codec" = "ansi" ]; then
                echo "$f regular ansi file, skip"
                continue
            fi
            dst_dir="$DST_DIR/$media_type/$codec/"
            [ $debug -eq 0 ] && [ ! -d "$dst_dir" ] && mkdir -p "$dst_dir" && echo "mkdir $dst_dir ...ok"
            link_file="$DST_DIR/$media_type/$codec/`basename \"$f\"`"
            if [ ! -f "$link_file" ];then
                [ $debug -eq 0 ] && ln -s "$f" "$dst_dir"
                ret=$?
                if [ $ret -eq 0 ]; then
                    echo "ln -s $f $dst_dir ...ok"
                elif [ $ret -eq 1 ]; then
                    #the link already existed
                    empty_exec=1
                else
                    echo "ln -s $f $dst_dir ...nok[$?]"
                fi
            fi
        done
    else
       echo "$f not media, skip"
    fi
    echo >&6
}&
done <<EOF
$file_list
EOF
wait
echo "`basename $0` make file index"
~/mk_index_list.sh
echo "`basename $0` ...finish"
